package org.example.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.util.StringUtils;
import net.sf.jsqlparser.expression.Expression;

/**
 * @Author: tang
 * @Description:
 * @Date 2025/6/5 23:19
 */
@Configuration
public class MybatisConfig {

    @Value("${multiTenant}")
    private String multiTenant;

    @Order(-3)
    @Bean
    public MasterSlaveAutoRoutingPluginCopy masterSlaveAutoRoutingPlugin() {
        return new MasterSlaveAutoRoutingPluginCopy();
    }

    @Order(-2)
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new MyPaginationInnerInterceptor());
        return mybatisPlusInterceptor;
    }

    @Bean
    @Order(-1)
    public MybatisPlusInterceptor getMybatisInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        if (StringUtils.hasText(this.multiTenant) && this.multiTenant.equalsIgnoreCase("true")) {
            interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
                public Expression getTenantId() {
                    return new LongValue(1L);
                }

                public String getTenantIdColumn() {
                    return "TenantId";
                }

                public boolean ignoreTable(String tableName) {
                    return false;
                }
            }));
        }

        return interceptor;
    }

//    @Bean
//    public MasterSlaveAutoRoutingPlugin masterSlaveAutoRoutingPlugin() {
//        return new MasterSlaveAutoRoutingPlugin();
//    }


}
